// pages/game2048/game2048.js
var grids = [
  [0, 0, 0, 0],
  [0, 0, 0, 0],
  [0, 0, 0, 0],
  [0, 0, 0, 0],
];
Page({
  data:{
    grids: grids,
    grids_show: grids,
    start_x: 0,
    start_y: 0,
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    this.fill_blank_randomly();
    this.setData({
      grids_show: this.data.grids,
    });
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  swap: function(grids_temp, i1, j1, i2, j2){
    var temp = grids_temp[i1][j1];
    grids_temp[i1][j1] = grids_temp[i2][j2];
    grids_temp[i2][j2] = temp;
    return grids_temp;
  },
  sweep_start: function(e) {
    this.setData({
      start_x: e.touches[0].pageX,
      start_y: e.touches[0].pageY,
    });
  },
  sweep_end: function(e) {
    var offset_x = e.changedTouches[0].pageX - this.data.start_x;
    var offset_y = e.changedTouches[0].pageY - this.data.start_y;
    if (Math.abs(offset_x) < Math.abs(offset_y)) {
      if (0 < offset_y) { //页面中x坐标向右为正，y坐标向下为正
        this.down_remove_blank();
        this.down_merge();
      } else {
        this.up_remove_blank();
        this.up_merge();
      }
    } else {
      if (0 < offset_x) {
        this.right_remove_blank();
        this.right_merge();
      } else {
        this.left_remove_blank();
        this.left_merge();
      }
    }
    this.valid_move_judge();
  },
  valid_move_judge: function(){
    var valid = false;
    for (var i in this.data.grids_show) {
      for (var j in this.data.grids_show[i]) {
        if (this.data.grids[i][j] != this.data.grids_show[i][j]) {
          valid = true;
          break;
        }
      }
      if (valid) {
        break;
      }
    }
    if (valid) {
      this.fill_blank_randomly();
      this.setData({ //前进一步，将此步写入矩阵展示中
        grids_show: this.data.grids,
      });
    } else {
      this.setData({ //放弃改变
        grids: this.data.grids_show,
      });
      console.log(valid);
    }
  },
  up_merge: function(){
    var grids_temp = this.data.grids;
    for (var j in grids_temp[0]) {
      for (var i=0; i<grids_temp.length-1; i++) {
        if (0 != grids_temp[i][j] && grids_temp[i][j] == grids_temp[i+1][j]) {
          grids_temp[i][j] += grids_temp[i+1][j];
          grids_temp[i+1][j] = 0;
          this.setData({
            grids: grids_temp,
          });
          this.up_remove_blank();
        }
      }
    }
  },
  down_merge: function(){
    var grids_temp = this.data.grids;
    for (var j in grids_temp[0]) {
      for (var i=grids_temp.length-1; i>0; i--) {
        if (0 != grids_temp[i][j] && grids_temp[i][j] == grids_temp[i-1][j]) {
          grids_temp[i][j] += grids_temp[i-1][j];
          grids_temp[i-1][j] = 0;
          this.setData({
            grids: grids_temp,
          });
          this.down_remove_blank();
        }
      }
    }
  },
  left_merge: function(){
    var grids_temp = this.data.grids;
    for (var i in grids_temp) {
      for (var j=0; j<grids_temp[i].length-1; j++) {
        if (0 != grids_temp[i][j] && grids_temp[i][j] == grids_temp[i][j+1]) {
          grids_temp[i][j] += grids_temp[i][j+1];
          grids_temp[i][j+1] = 0;
          this.setData({
            grids: grids_temp,
          });
          this.left_remove_blank();
        }
      }
    }
  },
  right_merge: function(){
    var grids_temp = this.data.grids;
    for (var i in grids_temp) {
      for (var j=grids_temp[i].length-1; j>0; j--) {
        if (0 != grids_temp[i][j] && grids_temp[i][j] == grids_temp[i][j-1]) {
          grids_temp[i][j] += grids_temp[i][j-1];
          grids_temp[i][j-1] = 0;
          this.setData({
            grids: grids_temp,
          });
          this.right_remove_blank();
        }
      }
    }
  },
  up_remove_blank: function() {
    var grids_temp = this.data.grids;
    for (var j in grids_temp[0]) {
      for (var i in grids_temp) {
        if (0 == grids_temp[i][j]) {
          continue;
        }
        var k = i;
        while (k-1>=0 && grids_temp[k-1][j] == 0) {
          grids_temp = this.swap(grids_temp, k, j, k-1, j);
          k--;
        }
      }
    }
    this.setData({
      grids: grids_temp,
    });
  },
  down_remove_blank: function() {
    var grids_temp = this.data.grids;
    for (var j in grids_temp[0]) {
      for (var i=grids_temp.length-2; i>=0; i--) {
        if (0 == grids_temp[i][j]) {
          continue;
        }
        var k = i;
        while (k+1<=grids_temp.length-1 && grids_temp[k+1][j] == 0) {
          grids_temp = this.swap(grids_temp, k, j, k+1, j);
          k++;
        }
      }
    }
    this.setData({
      grids: grids_temp,
    });
  },
  left_remove_blank: function() {
    var grids_temp = this.data.grids;
    for (var i in grids_temp) {
      for (var j in grids_temp[i]) {
        if (0 == grids_temp[i][j]) {
          continue;
        }
        var k = j;
        while (k-1>=0 && grids_temp[i][k-1] == 0) {
          grids_temp = this.swap(grids_temp, i, k, i, k-1);
          k--;
        }
      }
    }
    this.setData({
      grids: grids_temp,
    });
  },
  right_remove_blank: function() {
    var grids_temp = this.data.grids;
    for (var i in grids_temp) {
      for (var j=grids_temp[i].length-2; j>=0; j--) {
        if (0 == grids_temp[i][j]) {
          continue;
        }
        var k = j;
        while (k+1<=grids_temp[i].length-1 && grids_temp[i][k+1] == 0) {
          grids_temp = this.swap(grids_temp, i, k, i, k+1);
          k++;
        }
      }
    }
    this.setData({
      grids: grids_temp,
    });
  },
  generate_number: function(){
    if (0.6 < Math.random()) {
      return 4;
    } else {
      return 2;
    }
  },
  fill_blank_randomly: function() {
    var input = this.generate_number();
    var total_blank = 0;
    var filled = false;
    var _i;
    var _j;
    for (var i in this.data.grids) {
      for (var j in this.data.grids[i]) {
        if (0 == this.data.grids[i][j]) {
          total_blank += 1;
        }
      }
    }
    for (var i in this.data.grids) {
      for (var j in this.data.grids[i]) {
        if (0 == this.data.grids[i][j]) {
          _i = i;
          _j = j;
          if (Math.random() < 1/total_blank) {
            var grids_temp = this.data.grids;
            grids_temp[i][j] = input;
            this.setData({
              grids: grids_temp,
            });
            filled = true;
            break;
          }
        }
      }
      if (filled) {
        break;
      }
    }
    if (!filled) { //如果遍历了所有空格还没填，就将最后一个空格填上新产生的数
      var grids_temp = this.data.grids;
      grids_temp[_i][_j] = input;
      this.setData({
        grids: grids_temp,
      });
    }
  },
});